package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Wxblqsensor;
import com.example.onlinemonitoring.model.Wxblqhistory;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.WxblqsensorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * 
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "无线避雷器设备", tags = { "|无线避雷器设备|设备接口" })
@RestController
@RequestMapping("/Wirelesslightningarrester")
public class WxblqsensorController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    WxblqsensorServiceImpl wxblqsensorServiceImpl;

    @ApiOperation(value = "修改设备参数", notes = "修改设备参数")
    @ResponseBody
    @RequestMapping(value = "/updateAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAll(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Wxblqsensor record = object.toJavaObject(Wxblqsensor.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        if (wxblqsensorServiceImpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "修改设备报警", notes = "修改设备报警")
    @ResponseBody
    @RequestMapping(value = "/updateAlarm", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAlarm(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        // Wxblqsensor record = object.toJavaObject(Wxblqsensor.class);
        Wxblqsensor record = new Wxblqsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setArcurrentalarmrelievevalue(object.getFloat("arcurrentalarmrelievevalue"));
        record.setArcurrentalarmvalue(object.getFloat("arcurrentalarmvalue"));
        record.setArcurrentwarnrelievevalue(object.getFloat("arcurrentwarnrelievevalue"));
        record.setArcurrentwarnvalue(object.getFloat("arcurrentwarnvalue"));
        record.setAvacurrentalarmrelievevalue(object.getFloat("avacurrentalarmrelievevalue"));
        record.setAvacurrentalarmvalue(object.getFloat("avacurrentalarmvalue"));
        record.setAvacurrentwarnrelievevalue(object.getFloat("avacurrentwarnrelievevalue"));
        record.setAvacurrentwarnvalue(object.getFloat("avacurrentwarnvalue"));
        record.setAvbcurrentalarmrelievevalue(object.getFloat("avbcurrentalarmrelievevalue"));
        record.setAvbcurrentalarmvalue(object.getFloat("avbcurrentalarmvalue"));
        record.setAvbcurrentwarnrelievevalue(object.getFloat("avbcurrentwarnrelievevalue"));
        record.setAvbcurrentwarnvalue(object.getFloat("avbcurrentwarnvalue"));
        record.setAvccurrentalarmrelievevalue(object.getFloat("avccurrentalarmrelievevalue"));
        record.setAvccurrentalarmvalue(object.getFloat("avccurrentalarmvalue"));
        record.setAvccurrentwarnrelievevalue(object.getFloat("avccurrentwarnrelievevalue"));
        record.setAvccurrentwarnvalue(object.getFloat("avccurrentwarnvalue"));
        record.setAwcurrentalarmrelievevalue(object.getFloat("awcurrentalarmrelievevalue"));
        record.setAwcurrentalarmvalue(object.getFloat("awcurrentalarmvalue"));
        record.setAwcurrentwarnrelievevalue(object.getFloat("awcurrentwarnrelievevalue"));
        record.setAwcurrentwarnvalue(object.getFloat("awcurrentwarnvalue"));
        record.setBrcurrentalarmrelievevalue(object.getFloat("brcurrentalarmrelievevalue"));
        record.setBrcurrentalarmvalue(object.getFloat("brcurrentalarmvalue"));
        record.setBrcurrentwarnrelievevalue(object.getFloat("brcurrentwarnrelievevalue"));
        record.setBrcurrentwarnvalue(object.getFloat("brcurrentwarnvalue"));
        record.setBwcurrentalarmrelievevalue(object.getFloat("bwcurrentalarmrelievevalue"));
        record.setBwcurrentalarmvalue(object.getFloat("bwcurrentalarmvalue"));
        record.setBwcurrentwarnrelievevalue(object.getFloat("bwcurrentwarnrelievevalue"));
        record.setBwcurrentwarnvalue(object.getFloat("bwcurrentwarnvalue"));
        record.setCrcurrentalarmrelievevalue(object.getFloat("crcurrentalarmrelievevalue"));
        record.setCrcurrentalarmvalue(object.getFloat("crcurrentalarmvalue"));
        record.setCrcurrentwarnrelievevalue(object.getFloat("crcurrentwarnrelievevalue"));
        record.setCrcurrentwarnvalue(object.getFloat("crcurrentwarnvalue"));
        record.setCwcurrentalarmrelievevalue(object.getFloat("cwcurrentalarmrelievevalue"));
        record.setCwcurrentalarmvalue(object.getFloat("cwcurrentalarmvalue"));
        record.setCwcurrentwarnrelievevalue(object.getFloat("cwcurrentwarnrelievevalue"));
        record.setCwcurrentwarnvalue(object.getFloat("cwcurrentwarnvalue"));
        record.setHumidityalarmrelievevalue(object.getFloat("humidityalarmrelievevalue"));
        record.setHumidityalarmvalue(object.getFloat("humidityalarmvalue"));
        record.setHumiditywarnrelievevalue(object.getFloat("humiditywarnrelievevalue"));
        record.setHumiditywarnvalue(object.getFloat("humiditywarnvalue"));
        record.setTemperaturealarmrelievevalue(object.getFloat("temperaturealarmrelievevalue"));
        record.setTemperaturealarmvalue(object.getFloat("temperaturealarmvalue"));
        record.setTemperaturewarnrelievevalue(object.getFloat("temperaturewarnrelievevalue"));
        record.setTemperaturewarnvalue(object.getFloat("temperaturewarnvalue"));
        record.setThundercountalarmrelievevalue(object.getFloat("thundercountalarmrelievevalue"));
        record.setThundercountalarmvalue(object.getInteger("thundercountalarmvalue"));
        record.setThundercountbalarmrelievevalue(object.getFloat("thundercountbalarmrelievevalue"));
        record.setThundercountbalarmvalue(object.getInteger("thundercountbalarmvalue"));
        record.setThundercountbwarnrelievevalue(object.getFloat("thundercountbwarnrelievevalue"));
        record.setThundercountbwarnvalue(object.getInteger("thundercountbwarnvalue"));
        record.setThundercountcalarmrelievevalue(object.getFloat("thundercountcalarmrelievevalue"));
        record.setThundercountcalarmvalue(object.getInteger("thundercountcalarmvalue"));
        record.setThundercountcwarnrelievevalue(object.getFloat("thundercountcwarnrelievevalue"));
        record.setThundercountcwarnvalue(object.getInteger("thundercountcwarnvalue"));
        record.setThundercountwarnrelievevalue(object.getFloat("thundercountwarnrelievevalue"));
        record.setThundercountwarnvalue(object.getInteger("thundercountwarnvalue"));
        if (wxblqsensorServiceImpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询实时表数据", notes = "查询实时表数据")
    @ResponseBody
    @GetMapping("/selectSensorData")
    public MyResult<Wxblqsensor> selectSensorData(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        Wxblqsensor record = new Wxblqsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        QueryObject qo = new QueryObject(page, limit);
        return wxblqsensorServiceImpl.getList(record, qo);
    }

    @ApiOperation(value = "修改监测点名称", notes = "修改监测点名称")
    @ResponseBody
    @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
            @RequestParam(value = "id", required = false) Long id) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Wxblqsensor record = new Wxblqsensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        if (wxblqsensorServiceImpl.Update(record, true) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询历史表数据", notes = "查询历史表数据")
    @ResponseBody
    @GetMapping("/selectHistoryData")
    public MyResult<Wxblqhistory> selectHistoryData(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount) {
        Wxblqhistory record = new Wxblqhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        return wxblqsensorServiceImpl.getHisList(record, pagebase);
    }

    @ApiOperation(value = "查询历史表曲线", notes = "查询历史表曲线")
    @ResponseBody
    @GetMapping("/selectHistoryCurve")
    public MyResult<chartres> selectHistoryCurve(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "lstr", required = false) String lstr) {
        Wxblqhistory record = new Wxblqhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        pagebase.setIsasc(true);
        return wxblqsensorServiceImpl.getHisCurveList(record, lstr, pagebase);
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToExcel")
    public void exportToExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime) throws IOException {
        Wxblqhistory record = new Wxblqhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        Workbook wb = wxblqsensorServiceImpl.writeToExcelByList(record, pagebase);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToSensorExcel")
    public void exportToSensorExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr) throws IOException {
        Wxblqsensor record = new Wxblqsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        Workbook wb = wxblqsensorServiceImpl.writeToExcelBySensorList(record);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
}
